package com.lly.songhigh.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bartoszlipinski.flippablestackview.FlippableStackView;
import com.bartoszlipinski.flippablestackview.StackPageTransformer;
import com.lly.songhigh.R;
import com.lly.songhigh.activity.SearchMVDetialsActivity;
import com.lly.songhigh.activity.SingerDetailActivity;
import com.lly.songhigh.activity.SongSheetDetailActivity;
import com.lly.songhigh.activity.YouLikeActivity;
import com.lly.songhigh.adapter.FlippableStackViewAdapter;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.RecommendRoot;
import com.lly.songhigh.volley.GsonRequest;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by 遇博 on 15/11/03.
 */
public class RecommendFragment extends BaseFragment implements FlippableStackViewAdapter.ClickToSkipListener {
    private FlippableStackView stackView;
    private FlippableStackViewAdapter stackViewAdapter;
    private ArrayList<RecommendRoot.DataEntity> dataEntityArrayList;
    private GsonRequest<RecommendRoot> gsonRequest;
    private VolleySingleton volleySingleton;
    private RecommendRoot.DataEntity datas;
    private String url = URL.URL_RECOMMEND;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend, null);
        stackView = (FlippableStackView) view.findViewById(R.id.stackView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData();
    }

    public void getData() {
        volleySingleton = VolleySingleton.getInstance();
        gsonRequest = new GsonRequest<>(Request.Method.GET, url, RecommendRoot.class, new Response.Listener<RecommendRoot>() {
            @Override
            public void onResponse(RecommendRoot response) {
                dataEntityArrayList = (ArrayList<RecommendRoot.DataEntity>) response.getData();
                stackViewAdapter = new FlippableStackViewAdapter(getActivity(), dataEntityArrayList);
                stackViewAdapter.setClickToSkipListener(RecommendFragment.this);
                // 初始化stack的层数以及方向
                stackView.initStack(dataEntityArrayList.size(), StackPageTransformer.Orientation.VERTICAL);
                stackView.setAdapter(stackViewAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(gsonRequest);
    }

    @Override
    public void onClickToSkip(RecommendRoot.DataEntity dataEntity) {
        switch (dataEntity.getAction().getType()) {
            case 5:
                Intent intent5 = new Intent(getActivity(), SongSheetDetailActivity.class);
                intent5.putExtra("id", dataEntity.getId());
                startActivity(intent5);
                break;
            case 13:
                Intent intent13 = new Intent(getActivity(), YouLikeActivity.class);
                intent13.putExtra("id", dataEntity.getId());
                intent13.putExtra("name", dataEntity.getName());
                intent13.putExtra("resouce", 13);
                intent13.putExtra("picUrl", dataEntity.getSmallPicUrls().get(0));
                startActivity(intent13);
                break;
            case 18:
                Intent intent18 = new Intent(getActivity(), SearchMVDetialsActivity.class);
                intent18.putExtra("_id", dataEntity.getId());
                startActivity(intent18);
                break;
            case 20:
                Intent intent20 = new Intent(getActivity(), SingerDetailActivity.class);
                intent20.putExtra("singerId", dataEntity.getId());
                startActivity(intent20);
                break;
            case 21:
                Intent intent21 = new Intent(getActivity(), YouLikeActivity.class);
                intent21.putExtra("picUrl", dataEntity.getPicUrl());
                intent21.putExtra("name", dataEntity.getName());
                intent21.putExtra("id", dataEntity.getId());
                intent21.putExtra("resouce", 21);
                startActivity(intent21);
                break;
            case 22:
                Intent intent22 = new Intent(getActivity(), YouLikeActivity.class);
                intent22.putExtra("name", dataEntity.getName());
                intent22.putExtra("resouce", 22);
                intent22.putExtra("picUrl", dataEntity.getSmallPicUrls().get(0));
                startActivity(intent22);
                break;
        }
    }
}

